package com.keyroy.game.puzzle_rpg.dialogs;

import org.eclipse.swt.SWT;
import org.eclipse.swt.custom.CTabFolder;
import org.eclipse.swt.custom.CTabItem;
import org.eclipse.swt.events.SelectionAdapter;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.layout.FillLayout;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swt.widgets.Text;

import com.keyroy.game.puzzle_rpg.models.interfaces.EventListener;
import com.keyroy.game.puzzle_rpg.models.world.Map;
import com.keyroy.game.puzzle_rpg.utils.FileUtil;
import com.keyroy.game.puzzle_rpg.utils.StringUtil;
import com.keyroy.game.puzzle_rpg.views.IconView;

public class MapEditor extends Shell {
	private Text name_text;
	private Text info_text;
	private Text icon_text;
	private IconView icon_view;

	/**
	 * Launch the application.
	 * 
	 * @param args
	 */
	public static void main(String args[]) {
		try {
			Display display = Display.getDefault();
			MapEditor shell = new MapEditor(display, null);
			shell.open();
			shell.layout();
			while (!shell.isDisposed()) {
				if (!display.readAndDispatch()) {
					display.sleep();
				}
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	/**
	 * @wbp.parser.constructor
	 */
	public MapEditor(Display display, final EventListener<Map> listener) {
		this(display, null, listener);
	}

	public MapEditor(Display display, Map map, final EventListener<Map> listener) {
		super(display, SWT.DIALOG_TRIM);

		Label lblNewLabel = new Label(this, SWT.NONE);
		lblNewLabel.setBounds(10, 13, 35, 17);
		lblNewLabel.setText("\u540D\u79F0 : ");

		name_text = new Text(this, SWT.BORDER);
		name_text.setBounds(51, 10, 383, 23);

		CTabFolder tabFolder = new CTabFolder(this, SWT.BORDER);
		tabFolder.setBounds(10, 65, 424, 164);
		tabFolder.setSelectionBackground(Display.getCurrent().getSystemColor(
				SWT.COLOR_TITLE_INACTIVE_BACKGROUND_GRADIENT));

		CTabItem tabItem = new CTabItem(tabFolder, SWT.NONE);
		tabItem.setText("\u7F29\u7565\u56FE");

		icon_view = new IconView(tabFolder);
		tabItem.setControl(icon_view);

		CTabItem tabItem_1 = new CTabItem(tabFolder, SWT.NONE);
		tabItem_1.setText("\u4FE1\u606F");

		Composite composite_1 = new Composite(tabFolder, SWT.NONE);
		tabItem_1.setControl(composite_1);
		composite_1.setLayout(new FillLayout(SWT.HORIZONTAL));

		info_text = new Text(composite_1, SWT.BORDER | SWT.WRAP);

		Button btnNewButton = new Button(this, SWT.NONE);
		btnNewButton.addSelectionListener(new SelectionAdapter() {
			@Override
			public void widgetSelected(SelectionEvent e) {
				if (listener != null) {
					Map map = new Map();
					map.name = name_text.getText();
					map.icon = icon_text.getText();
					map.info = info_text.getText();
					listener.onEvent(map);
				}
				dispose();
			}
		});
		btnNewButton.setBounds(354, 235, 80, 27);
		btnNewButton.setText("\u5B8C\u6210");

		Label lblNewLabel_1 = new Label(this, SWT.NONE);
		lblNewLabel_1.setBounds(10, 42, 35, 17);
		lblNewLabel_1.setText("\u56FE\u7247 : ");

		icon_text = new Text(this, SWT.BORDER | SWT.READ_ONLY);
		icon_text.setBounds(51, 39, 297, 23);

		Button button = new Button(this, SWT.NONE);
		button.addSelectionListener(new SelectionAdapter() {
			@Override
			public void widgetSelected(SelectionEvent e) {
				String path = FileUtil.selectFile(getShell(), null, null, new String[] { "*.png", "*.gif" });
				if (path != null) {
					icon_text.setText(path);
				}
				icon_view.setImage(path);
			}
		});
		button.setBounds(354, 37, 80, 27);
		button.setText("\u9009\u62E9\u56FE\u7247");
		tabFolder.setSelection(0);
		createContents();

		if (map != null) {
			name_text.setText(StringUtil.stringOf(map.name));
			info_text.setText(StringUtil.stringOf(map.info));
			icon_text.setText(StringUtil.stringOf(map.icon));
			icon_view.setImage(map.icon);
		}
	}

	/**
	 * Create contents of the shell.
	 */
	protected void createContents() {
		setText("\u5730\u56FE\u5C5E\u6027");
		setSize(450, 300);

	}

	@Override
	protected void checkSubclass() {
		// Disable the check that prevents subclassing of SWT components
	}

	public Composite getIconView() {
		return icon_view;
	}
}
